package com.liu.community.controller;/*
 *@Author:Liu Xing-zhi
 *Description:
 *Date:Crated in 10:57 2021/10/21
 */

import com.liu.community.dto.NotificationDto;
import com.liu.community.dto.PageDto;
import com.liu.community.dto.QuestionDto;
import com.liu.community.enums.NotificationTypeEnum;
import com.liu.community.pojor.User;
import com.liu.community.server.NotificationService;
import com.liu.community.utils.URLUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/notification/{id}")
    public  String goProfile(@PathVariable(name = "id")Integer id, Model model,
                             HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("user");
        if (user == null){
            return URLUtils.sendRedirectTo("/index");
        }
        //根据id，和user
        NotificationDto notificationDto  = notificationService.read(id,user);

        if (NotificationTypeEnum.REPLY_QUESTION.getType() == notificationDto.getType()){
            return URLUtils.sendRedirectTo("/question/" + notificationDto.getOuterId());
        }else  if (NotificationTypeEnum.REPLY_COMMENT.getType() == notificationDto.getType()){
            return URLUtils.sendRedirectTo("/comment/" + notificationDto.getOuterId());
        }else  if (NotificationTypeEnum.REPLY_BLOG.getType() == notificationDto.getType()){
            return URLUtils.sendRedirectTo("/blog/" + notificationDto.getOuterId());
        }else  if (NotificationTypeEnum.REPLY_BLOG_COMMENT.getType() == notificationDto.getType()){
            return URLUtils.sendRedirectTo("/blogComment/" + notificationDto.getOuterId());
        }
        else {
            return URLUtils.sendRedirectTo("/index");
        }
    }

}
